﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodeCreator.CoreObj;
using CodeCreator.Template;
using CodeCreator.CoreObj.DbManager;
using CodeCreator.DbHelper;

namespace CodeCreator.DbManager
{
    public partial class FrmCheckTables : Form
    {
        public IDbHelper dbHelper = null;
        public List<TypeMap> typeMapList = new List<TypeMap>();
        private List<String> tableList = new List<string>();
        public List<Obj> objList = new List<Obj>();

        public FrmCheckTables(IDbHelper dbHelper)
        {
            InitializeComponent();
            this.dbHelper = dbHelper;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.clbTables.CheckedItems.Count > 0)
            {
                objList.Clear();
                for (int i = 0; i < this.clbTables.CheckedItems.Count; i++)
                {
                    string schemaName = this.cmbSchema.SelectedItem.ToString();
                    string tableName = this.clbTables.CheckedItems[i].ToString();
                    Obj obj = this.dbHelper.GetColumnsByTable(schemaName,tableName,this.typeMapList);

                    //构建表的SQL
                    List<ObjSql> sqls = new List<ObjSql>();
                    sqls.Add(new ObjSql("select", getSelectSql(obj)));
                    sqls.Add(new ObjSql("insert", getInsertSql(obj)));
                    sqls.Add(new ObjSql("update", getUpdateSql(obj)));
                    sqls.Add(new ObjSql("delete", getDeleteSql(obj)));
                    obj.Sqls = sqls;

                    //添加到列表
                    objList.Add(obj);
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.No;
            }
        }

        private string getSelectSql(Obj table)
        {
            string result = "";
            if (table != null && table.Fields != null)
            {
                result = "select ";
                foreach (ObjField of in table.Fields)
                {
                    result += "\r\n\t" + of.DbFieldName + " as "+of.ClassFieldNameFirstLower+",";
                }
                if (result.EndsWith(","))
                    result = result.Substring(0, result.Length - 1);
                result += " from " + table.TableFullName;
                result += " where 1=1 ";
            }
            return result;
        }

        private string getDeleteSql(Obj table)
        {
            string result = "";
            if (table != null && table.Fields != null)
            {
                result = "delete from ";
                result += table.TableFullName;
                result += " where 1=1 and \r\n\t";
                foreach (ObjField of in table.Fields)
                {
                    if (of.DbFieldPk)
                    {
                        result += of.DbFieldName ;
                    }
                }
            }
            return result;
        }

        private string getInsertSql(Obj table)
        {
            string result = "";
            if (table != null && table.Fields != null)
            {
                string strColumns ="";
                string strValues="";
                foreach (ObjField of in table.Fields)
                {
                    strColumns += "\r\n\t" + of.DbFieldName + ",";
                    switch (of.DbFieldType)
                    {
                        case "NUMBER":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                            break;
                        case "LONG":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                            break;
                        case "INTEGER":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                            break;
                        case "FLOAT":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                            break;
                        case "TIMESTAMP":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=DATE},";
                            break;
                        case "TIMESTAMP(6)":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=DATE},";
                            break;
                        case "DATE":
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=DATE},";
                            break;
                        default:
                            strValues += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=VARCHAR},";
                            break;
                    }
                }
                if (strColumns.EndsWith(","))
                    strColumns = strColumns.Substring(0, strColumns.Length - 1);
                if (strValues.EndsWith(","))
                    strValues = strValues.Substring(0, strValues.Length - 1);
                
                result = "insert into ";
                result += table.TableFullName;
                result += " ("+strColumns+") ";
                result += " values ";
                result += "(" + strValues + ")";
            }
            return result;
        }

        private string getUpdateSql(Obj table)
        {
            string result = "";
            if (table != null && table.Fields != null)
            {
                string strColumns = "";
                string strWhere = "";
                foreach (ObjField of in table.Fields)
                {
                    if (of.DbFieldPk)
                    {
                        strWhere += "\r\n\t" + of.DbFieldName + "=";
                        strWhere += "#{" + of.ClassFieldNameFirstLower + "} and";
                    }
                    else
                    {
                        strColumns += "\r\n\t" + of.DbFieldName + "=";
                        switch (of.DbFieldType)
                        {
                            case "NUMBER":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                                break;
                            case "LONG":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                                break;
                            case "INTEGER":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                                break;
                            case "FLOAT":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=NUMERIC},";
                                break;
                            case "TIMESTAMP":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=DATE},";
                                break;
                            case "TIMESTAMP(6)":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=DATE},";
                                break;
                            case "DATE":
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=DATE},";
                                break;
                            default:
                                strColumns += "\r\n\t#{" + of.ClassFieldNameFirstLower + ",jdbcType=VARCHAR},";
                                break;
                        }
                    }
                }
                if (strColumns.EndsWith(","))
                    strColumns = strColumns.Substring(0, strColumns.Length - 1);
                if (strWhere.EndsWith("and"))
                    strWhere = strWhere.Substring(0, strWhere.Length - 3);

                result = "update ";
                result += table.TableFullName;
                result += " set " + strColumns + " ";
                result += " where "+strWhere;
            }
            return result;
        }

        private string objectToString(object obj)
        {
            if (obj != null)
                return obj.ToString();
            else
                return "";
        }       

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.No;
        }

        private void FrmCheckTablesForOracle_Load(object sender, EventArgs e)
        {
            List<string> schemas = this.dbHelper.GetAllSchema();
            this.cmbSchema.Items.Clear();
            foreach (string schema in schemas)
            {
                this.cmbSchema.Items.Add(schema);
            }
            if (this.cmbSchema.Items.Count > 0)
                this.cmbSchema.SelectedIndex = 0;
        }

        private void cmbSchema_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbSchema.SelectedIndex >= 0)
            {
                string strSchema = this.cmbSchema.SelectedItem.ToString();
                List<string> tables = this.dbHelper.GetTablesBySchema(strSchema);
                tableList.Clear();
                this.clbTables.Items.Clear();
                foreach (string table in tables)
                {
                    tableList.Add(table);
                    this.clbTables.Items.Add(table);
                }
            }
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            if (this.tableList != null && this.tableList.Count > 0)
            {
                string strFilter = this.txtFilter.Text.Trim();
                this.clbTables.Items.Clear();
                foreach (string str in this.tableList)
                {
                    if (str.ToUpper().IndexOf(strFilter.ToUpper()) >= 0)
                    {
                        this.clbTables.Items.Add(str);
                    }
                }
            }
        }
    }
}
